// $codepro.audit.disable numericLiterals, packagePrefixNamingConvention
/**
 * package for all of the universe controller
 */
package javamonsters.universe.application;

import java.awt.Point;
import java.math.BigDecimal;

import javamonsters.controller.ShipYardController;
import javamonsters.model.Encounter;
import javamonsters.model.Marketplace;
import javamonsters.model.Planet;
import javamonsters.model.ShipYard;
import javamonsters.model.SolarSystem;
import javamonsters.model.Encounter.Encounters;
import javamonsters.universe.model.interfaces.UniverseModel;
import javamonsters.universe.presentation.DefaultUniverseView;
import javamonsters.universe.presentation.interfaces.UniverseView;
import javamonsters.view.SpaceTraders;
import javamonsters.view.UniverseSideView;

import javax.swing.JOptionPane;




/**
 * This is the Universe controller which controls things on the dual universe panel
 * 
 * @author Evan LaHurd
 * @version 1.0
 *
 */
public class UniverseController {

	/**
	 * reference to the universe model
	 */
	private UniverseModel universeModel;
	
	/**
	 * reference to universe view
	 */
	private final UniverseView universeView;
	
	/**
	 * reference to universe side view
	 */
	private final UniverseSideView sideView;
	
	/**
	 * reference to main game controller
	 */
	private final SpaceTraders parentController;
	
	/**
	 * credits to be taken in a random encounter
	 */
	private final int creditsTaken;
	
	/**
	 * credits to be received during a random encounter
	 */
	private final int creditsReceived;
	
	/**
	 * Universe controller constructor
	 * 
	 * @param universeModel the model for the controller
	 * @param universeView the universe map view for the controller
	 * @param sideView the side panel view for the controller 
	 * @param parentController the main game controller
	 */
	public UniverseController(UniverseModel universeModel, UniverseView universeView,
		   UniverseSideView sideView, SpaceTraders parentController) {
		this.universeModel = universeModel;
		this.universeView = universeView;
		this.sideView = sideView;
		universeView.setController(this);
		
		creditsTaken = 100;
		creditsReceived = 100;
		//universeView.addSolarSystemSelectionListener(new SolarSystemSelectionListener());
		universeView.plotSystems(universeModel.getSolarSystems());
		
		this.parentController = parentController;
		//main.getExitButton().addActionListener(new exitButtonListener());
	}
	
	/**
	 * @return String representation of Universe Controller
	 */
	public String toString() {
		return "universe controller";
	}
	
	/**
	 * Loads the SolarSystem so that the Universe can control it
	 * @param ss the SolarSystem object for which the Universe can control
	 */
	public void loadSystem(SolarSystem ss) {
		System.out.println("loading system");
		// make this a global or something; well, not global, but define it somewhere?
		// define all card names somewhere?
		
		// Tell the marketplace controller to set current character
		// set marketplace, setcharacter, populate
		// this shouldn't be here
		final Marketplace mkt = ss.getPlanets().get(0).getMarketplace();
		mkt.setCurrentCharacter(parentController.getPlayer());
		
		// random encounter check
		final Planet planet = ss.getPlanets().get(0);
		final Point newSysLoc = planet.getSystem().getCoordinates();
		final Point oldSysLoc = parentController.getPlayer().getCurrLocation();
		
		final Encounter newEncounter = new Encounter();
		
		if(newEncounter.hasEncounter()) {
			if(!newSysLoc.equals(oldSysLoc)) {
				//JOptionPane.showMessageDialog(null, ss.getPlanets().get(0).randEncounter());
				//System.out.println(ss.getPlanets().get(0).randEncounter());
				
				if(newEncounter.getEncounterType().compareTo(Encounters.PIRATE) == 0 ||
				   newEncounter.getEncounterType().compareTo(Encounters.POLICE) == 0) {
					 if (mkt.getCharacter().getCredits().intValue() < creditsTaken) {
						 mkt.getCharacter().setCredits(new BigDecimal(0));
					 } else {
						 mkt.getCharacter().removeCredits(new BigDecimal(creditsTaken));
					 }
				} else {
					mkt.getCharacter().addCredits(new BigDecimal(creditsReceived));
				}
				
				System.out.println(newEncounter.getEncounterType()
						.compareTo(Encounters.PIRATE));
				
				System.out.println(newEncounter.getEncounterType().toString());
				
				JOptionPane.showMessageDialog(null, newEncounter.randEncounter());
			}
		}
		
		mkt.setAssociatedShip(parentController.getPlayer().getShip());
		parentController.getMarketplaceController().setMarket(mkt);
		
		final ShipYard shipYard = ss.getPlanets().get(0).getShipYard();
		shipYard.setCurrentCharacter(parentController.getPlayer());
		
		final ShipYardController shipYardController = 
				parentController.getShipYardController();
		
		shipYardController.getShipYardView()
		.getShipListModel().clear();
		
		parentController.getShipYardController().setShipYard(shipYard);
		//parentController.getMarketplaceController().setCurrentCharacter();
		parentController.show("Marketplace View");

		// We're only adding one marketplaceview to the cardlayout
		// When we need to use it, we tell card to display it, as in the above line, and then
		// use a delegate method to set its "data source"

	}
	
	/**
	 * @return the main controller for the game
	 */
	public SpaceTraders getController() {
		return parentController;
	}
	
	/**
	 * @return return the universe model that this controller controls
	 */
	public UniverseModel getUniverseModel() {
		return universeModel;
	}
	
	/**
	 * @return the side panel view for the universe map
	 */
	public UniverseSideView getSideView() {
		return sideView;
	}
	
	/**
	 * @return the universe map view
	 */
	public DefaultUniverseView getUniverseView() {
		return (DefaultUniverseView) universeView;
	}
	
	/**
	 * sets the universe model
	 * 
	 * @param model the new universe model to be set
	 */
	public void setUniverseModel(UniverseModel model) {
		universeModel = model;
		universeView.plotSystems(universeModel.getSolarSystems());
	}
	
	/**
	 * sets the players current system to a new system
	 * 
	 * @param ss the new current system to be set 
	 */
	public void setCurrSystem(SolarSystem ss) {
		parentController.setCurrSystem(ss);
	}
}